package com.example.zhaoping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhaoping.entity.Enterprise;
import com.example.zhaoping.entity.PageParam;
import com.example.zhaoping.entity.Position;
import com.example.zhaoping.entity.User;
import com.example.zhaoping.serviceImpl.EnterpriseServiceImpl;
import com.example.zhaoping.serviceImpl.PositionSericeImpl;
import com.example.zhaoping.serviceImpl.UserServiceImpl;
import com.example.zhaoping.util.Result;
import com.example.zhaoping.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class PositionController {
    @Autowired
    private Result result;
    @Resource
    private PositionSericeImpl positionSerice;
    @Resource
    private EnterpriseServiceImpl enterpriseService;
    @Resource
    private UserServiceImpl userService;

    @GetMapping(value = "/get_user",name = "获取求职者实例")
    public Result getUser(@RequestHeader("Authorization") String token){
        int id = TokenUtil.getUserId(token);
        User user = userService.selectByUserId(id);
        if(user!=null){
            return result.success(user);
        }else {
            return result.error("获取用户失败");
        }
    }

    @PostMapping(value = "/getPosition", name = "获取职位列表")
    public Result getPosition(PageParam param) {
        Page page = positionSerice.selectList(param);
        return result.success(page);
    }

    @GetMapping(value = "/getJobDetails",name = "获取职位详情")
    public Result getJobDetails(@RequestParam int id){
        Position position = positionSerice.getPosition(id);
        Enterprise enterprise = enterpriseService.selectOne(position.getUsername());
        position.setEnterpriseList(enterprise);
        return result.success(position);
    }
}
